﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KpBusiness.Model;
using NHibernate;
using log4net;
using KpBusiness.Layer;
using NHibernate.Criterion;


namespace KpBusiness.Service
{
    public class RoleService
    {
        private static readonly ILog log = LogManager.GetLogger("Services");

        /// <summary>
        /// Get Role
        /// </summary>
        /// <param name="roleId">Provide Role Number</param>
        /// <returns>Returns Role</returns>
        public Role GetRole(int roleId)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            Role role = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                role = session.Get<Role>(roleId);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return role;
        }

        /// <summary>
        /// Creates New Role in DB
        /// </summary>
        /// <param name="role">Provide Role</param>
        public void SaveRole(Role role)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenWriterSession();
           
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                session.SaveOrUpdate(role);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
          
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<Role> GetAllRoles()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<Role> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(Role));
                students = criteria.List<Role>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }

        /// <summary>
        /// Get All Active Roles
        /// </summary>
        /// <returns></returns>
        public IList<Role> GetAllActiveRoles()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<Role> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(Role))
                    .Add(Restrictions.Eq("Role.IsActive", true));
                    
                students = criteria.List<Role>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }

    }
}
